// Copyright (c) 2020 Spotify AB.
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

import Foundation

/// Methods on `String` that remove build specific information from the log.
///
/// This can be useful if we want to group logs by its content in order to indicate
/// how often particular log occurs.
extension String {
    /// Removes autogenerated build identifier in built product path.
    ///
    /// Example: "DerivedData/Product-bolnckhlbzxpxoeyfujluasoupft/Build" becomes "DerivedData/Product/Build".
    func removeProductBuildIdentifier() -> String {
        do {
            var mutableSelf = self
            let regularExpression = try NSRegularExpression(pattern: "/DerivedData/(.*)-(.*)/Build/")
            regularExpression.enumerateMatches(in: self,
                                               options: [],
                                               range: NSRange(location: 0, length: count)) { match, _, _ in
                if let match = match, match.numberOfRanges == 3 {
                    let buildIdentifier = self.substring(match.range(at: 2))
                    mutableSelf = mutableSelf.replacingOccurrences(of: "-" + buildIdentifier, with: "")
                }
            }
            return mutableSelf
        } catch {
            return self
        }
    }

    /// Removes hexadecimal numbers from the log and puts `<hexadecimal_number>` instead.
    ///
    /// Example: "NSUnderlyingError=0x7fcdc8712290" becomes "NSUnderlyingError=<hexadecimal_number>".
    func removeHexadecimalNumbers() -> String {
        do {
            var mutableSelf = self
            let regularExpression = try NSRegularExpression(pattern: "0[xX][0-9a-fA-F]+")
            regularExpression.enumerateMatches(in: self,
                                               options: [],
                                               range: NSRange(location: 0, length: count)) { match, _, _ in
                if let match = match {
                    let hexadecimalNumber = self.substring(match.range(at: 0))
                    mutableSelf = mutableSelf.replacingOccurrences(of: hexadecimalNumber, with: "<hexadecimal_number>")
                }
            }
            return mutableSelf
        } catch {
            return self
        }
    }
}
